package com.xinghai.service.impl;


import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xinghai.common.utils.DateUtils;
import com.xinghai.domain.*;
import com.xinghai.mapper.JkqActivityShareMapper;
import com.xinghai.mapper.JkqActivitySignMapper;
import com.xinghai.mapper.JkqUserBadgeMapper;
import com.xinghai.service.IJkqUserBadgeService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import com.xinghai.mapper.JkqBadgeMapper;
import com.xinghai.service.IJkqBadgeService;

import javax.annotation.Resource;

/**
 * 徽章管理Service业务层处理
 *
 * @author xinghai
 * @date 2025-08-02
 */
@Service
public class JkqBadgeServiceImpl extends MPJBaseServiceImpl<JkqBadgeMapper, JkqBadge>
        implements IJkqBadgeService
{

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Resource
    private IJkqUserBadgeService userBadgeService;

    @Resource
    private JkqActivityShareMapper activityShareMapper;

    @Resource
    private JkqActivitySignMapper activitySignMapper;
    /**
     * 查询徽章管理列表
     *
     * @param jkqBadge 徽章管理
     * @return 徽章管理集合
     */
    @Override
    public List<JkqBadge> list(JkqBadge jkqBadge) {
        QueryWrapper<JkqBadge> queryWrapper = new QueryWrapper<>();
        if (jkqBadge.getName()!= null  && !jkqBadge.getName().isEmpty()){
            queryWrapper.like("name", jkqBadge.getName());
        }
        if (jkqBadge.getLimitType()!= null  && !jkqBadge.getLimitType().isEmpty()){
            queryWrapper.eq("limit_type", jkqBadge.getLimitType());
        }
        if (jkqBadge.getRemark()!= null  && !jkqBadge.getRemark().isEmpty()){
            queryWrapper.like("remark", jkqBadge.getRemark());
        }
        if (jkqBadge.getStatus()!= null  && !jkqBadge.getStatus().isEmpty()){
            queryWrapper.eq("status", jkqBadge.getStatus());
        }
        return this.list(queryWrapper);
    }

    /**
     * 导入徽章管理信息
     *
     * @param list 徽章管理信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<JkqBadge> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            JkqBadgeMapper jkqBadgeMapper = sqlSession.getMapper(JkqBadgeMapper.class);
            list.forEach(jkqBadgeMapper::insert);
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }

    @Override
    @Transactional
    public void getBadgePower(BadgeItem badgeItem) {
        JkqBadge jkqBadge = new JkqBadge();
        jkqBadge.setLimitType(badgeItem.getType());
        jkqBadge.setStatus("0");
        List<JkqBadge> list = list(jkqBadge);
        for (JkqBadge badge : list){
            //活跃天数
            if ("1".equals(badge.getLimitType())){
                if (badgeItem.getNum() >= badge.getLimitActiveNum()){
                    badgeItem.setBadgeId(badge.getId());
                    userBadgeService.getBadgePower(badgeItem);
                    continue;
                }
            }
            //分享次数
            if ("2".equals(badge.getLimitType())){
                Long count = activityShareMapper.selectCount(new QueryWrapper<JkqActivityShare>()
                        .eq("user_id", badgeItem.getUserId()));
                if (count >= badge.getLimitShareNum()){
                    badgeItem.setBadgeId(badge.getId());
                    userBadgeService.getBadgePower(badgeItem);
                    continue;
                }
            }
            //参加次数
            if ("3".equals(badge.getLimitType())){
                Long count = activitySignMapper.selectCount(new QueryWrapper<JkqActivitySign>()
                        .eq("user_id", badgeItem.getUserId()));
                if (count >= badge.getLimitActivityNum()){
                    badgeItem.setBadgeId(badge.getId());
                    userBadgeService.getBadgePower(badgeItem);
                    continue;
                }
            }
            //参加成功次数
            if ("4".equals(badge.getLimitType())){
                Long count = activitySignMapper.selectCount(new QueryWrapper<JkqActivitySign>()
                        .eq("status", "1")
                        .eq("user_id", badgeItem.getUserId()));
                if (count >= badge.getLimitActivitySucessNum()){
                    badgeItem.setBadgeId(badge.getId());
                    userBadgeService.getBadgePower(badgeItem);
                    continue;
                }
            }
            if ("5".equals(badge.getLimitType())){

            }
            if ("6".equals(badge.getLimitType())){

            }
        }
    }
}
